//多线程求奇偶数之和
class SumOperator
{
    long sum1=0;
    long sum2=0;
    synchronized  public void addsum1(int num)
    {
        sum1=sum1+num;
    }
    synchronized public void addsum2(int num)
    {
        sum2=sum2+num;
    }

    synchronized public long sum()
    {
        System.out.println("偶数和：" + sum1);
        System.out.println("奇数和：" + sum2);
        return sum1+sum2;
    }
}
public class ThreadDemo13 {


    public static void main(String[] args) throws InterruptedException {
        //计算开始时间
        long start=System.currentTimeMillis();
        int total=1000_0000;
        int[] array =new int[total];
        for (int i = 0; i < total; i++) {
            array[i]= (int)(Math.random()*100);
        }

        SumOperator  sumOperator=new SumOperator();
        Thread t1 =new Thread( () -> {
            for (int i = 0; i < total; i=i+2) {
                sumOperator.addsum1(array[i]);
            }
        });
        Thread t2 =new Thread( () -> {
            for (int i = 1; i < total; i=i+2) {
                sumOperator.addsum2(array[i]);
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        long end=System.currentTimeMillis();
        System.out.println("时间为:"+(end-start)+"ms");
        System.out.println("和为"+sumOperator.sum());
    }


}
